package metagen;

import main.MetaGen;
import util.GenContext;


public class Capture implements MetaGen {

    private MetaGen meta;
    private int idx;
    private GenContext ctx;

    public Capture(GenContext ctx, int idx, MetaGen meta) {
        if (meta == null) {
            throw new RuntimeException("Regexp to be captured must be non-null!");
        }
        this.meta = meta;
        this.ctx = ctx;
        this.idx = idx;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("(")
                .append(meta.toString())
                .append(")");
        return sb.toString();
    }

    public String randGen() {
        String s = meta.randGen();
        ctx.setCapture(idx, s);
        return s;
    }

    public void setMeta(MetaGen meta) {
        this.meta = meta;
    }

    public void setIdx(int idx) {
        this.idx = idx;
    }

    public void setCtx(GenContext ctx) {
        this.ctx = ctx;
    }
}
